package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.WithdrawalAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.EdcEntity;
import com.glory.brepda.entity.WithdrawalEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WithdrawalActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvDataList)
    RecyclerView rvDataList;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.flWarehoseList)
    FrameLayout flWarehoseList;
    @BindView(R.id.tvWarehose)
    TextView tvWarehose;
    @BindView(R.id.ivWarehose)
    ImageView ivWarehose;
    @BindView(R.id.flStoragePackList)
    FrameLayout flStoragePackList;
    @BindView(R.id.tvStoragePack)
    TextView tvStoragePack;
    @BindView(R.id.ivStoragePack)
    ImageView ivStoragePack;
    @BindView(R.id.flStoragePackareaList)
    FrameLayout flStoragePackareaList;
    @BindView(R.id.tvStoragePackarea)
    TextView tvStoragePackarea;
    @BindView(R.id.ivStoragePackarea)
    ImageView ivStoragePackarea;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    private WithdrawalAdapter withdrawalAdapter;
    private int number = 0;
    private FreeUI_GeneralPop warehosePop;
    private FreeUI_GeneralPop storagePackPop;
    private FreeUI_GeneralPop storagePackareaPop;
    private ArrayList<String> warehoseList = new ArrayList<>();
    private ArrayList<String> warehoseRRN = new ArrayList<>();
    private ArrayList<String> storagePackList = new ArrayList<>();
    private ArrayList<String> storagePackRRN = new ArrayList<>();
    private ArrayList<String> StoragePackareaList = new ArrayList<>();
    private ArrayList<String> StoragePackareaRRN = new ArrayList<>();
    private String warehoseCode = "";
    private String storagePackCode = "";
    private String StoragePackareaCode = "";
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_withdrawal;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText(String.valueOf(number));
        withdrawalAdapter = new WithdrawalAdapter(null);
        withdrawalAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        withdrawalAdapter.isFirstOnly(true);
        initRecyclerView(rvDataList, this);
        rvDataList.setAdapter(withdrawalAdapter);

    }

    @Override
    protected void initData() {

        initEdit(etLotID, s->{
            FreeUI_EntityCallBack<BaseResponseBean<WithdrawalEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<WithdrawalEntity>>
                    (new TypeToken<BaseResponseBean<WithdrawalEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<WithdrawalEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        if (response.body().getResponse().getBody().getMLOTLIST() == null){
                            showToast("获取信息失败，请联系系统管理员",false);
                        }else {
                            withdrawalAdapter.addData(response.body().getResponse().getBody().getMLOTLIST());
                            tvBaseSubmit.setText(String.valueOf(withdrawalAdapter.getData().size()));
                            showToast("操作成功",true);
                            etLotID.setText("");
                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<WithdrawalEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(),"MLOTBACK");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE","MLOTRETURN");
            map.addProperty("MLOTID",s);
            map.addProperty("WAREHOUSERRN",warehoseCode);
            map.addProperty("STORAGEPACKRRN",storagePackCode);
            map.addProperty("STORAGEPACKAREARRN",StoragePackareaCode);
            NetUtils.requestPostNet("IF.MLOTBACK","Common", WithdrawalActivity.this,map,callBack);
        });

        FreeUI_EntityCallBack<BaseResponseBean<WithdrawalEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<WithdrawalEntity>>
                (new TypeToken<BaseResponseBean<WithdrawalEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<WithdrawalEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    for (WithdrawalEntity.MmwarehouseList data:response.body().getResponse().getBody().getMMWAREHOUSELIST()){
                        warehoseList.add(data.getWAREHOUSEID()+"/"+data.getDESCRIPTION()+"/"+data.getWAREHOUSETYPE());
                        warehoseRRN.add(data.getOBJECTRRN());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<WithdrawalEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"MLOTBACK");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QEURYWAREHOSE");
        NetUtils.requestPostNet("IF.MLOTBACK","Common", WithdrawalActivity.this,map,callBack);
    }

    @OnClick({R.id.tvBaseSubmit,R.id.flWarehoseList,R.id.flStoragePackList,R.id.flStoragePackareaList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                break;
            case R.id.flWarehoseList:
                if (warehosePop != null) {
                    if (warehosePop.isShowing()) {
                        warehosePop.dismiss();
                    }
                } else {
                    warehosePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flWarehoseList.getWidth(), warehoseList, ivWarehose, (codeS, i) -> {
                        tvWarehose.setText(codeS);
                        getStoragePack(warehoseRRN.get(i));
                        warehoseCode = warehoseRRN .get(i);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivWarehose);
                warehosePop.showAsDropDown(flWarehoseList, 0, 0);
                break;
            case R.id.flStoragePackList:
                if (storagePackPop != null) {
                    if (storagePackPop.isShowing()) {
                        storagePackPop.dismiss();
                    }
                } else {
                    storagePackPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flStoragePackList.getWidth(), storagePackList, ivStoragePack, (codeS, i) -> {
                        tvStoragePack.setText(codeS);
                        getStoragePackarea(storagePackRRN.get(i));
                        storagePackCode = storagePackRRN.get(i);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivStoragePack);
                storagePackPop.showAsDropDown(flStoragePackList, 0, 0);
                break;
            case R.id.flStoragePackareaList:
                if (storagePackareaPop != null) {
                    if (storagePackareaPop.isShowing()) {
                        storagePackareaPop.dismiss();
                    }
                } else {
                    storagePackareaPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flStoragePackareaList.getWidth(), StoragePackareaList, ivStoragePackarea, (codeS, i) -> {
                        tvStoragePackarea.setText(codeS);
                        StoragePackareaCode = StoragePackareaRRN.get(i);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivStoragePackarea);
                storagePackareaPop.showAsDropDown(flStoragePackareaList, 0, 0);
                break;
        }
    }

    private void getStoragePackarea(String rrn) {
        StoragePackareaList.clear();
        StoragePackareaRRN.clear();
        FreeUI_EntityCallBack<BaseResponseBean<WithdrawalEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<WithdrawalEntity>>
                (new TypeToken<BaseResponseBean<WithdrawalEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<WithdrawalEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    for (WithdrawalEntity.MMstorageRackareaList data:response.body().getResponse().getBody().getMMSTORAGERACKAREALIST()){
                        StoragePackareaList.add(data.getNAME()+"/"+data.getDESCRIPTION()+"/"+data.getCATEGORY());
                        StoragePackareaRRN.add(data.getOBJECTRRN());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<WithdrawalEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"MLOTBACK");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERYSTORAGEPACKAREA");
        map.addProperty("STORAGEPACKRRN",rrn);
        NetUtils.requestPostNet("IF.MLOTBACK","Common", WithdrawalActivity.this,map,callBack);
    }

    private void getStoragePack(String rrn) {
        storagePackList.clear();
        storagePackRRN.clear();
        FreeUI_EntityCallBack<BaseResponseBean<WithdrawalEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<WithdrawalEntity>>
                (new TypeToken<BaseResponseBean<WithdrawalEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<WithdrawalEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    for (WithdrawalEntity.MMstorageRackList data:response.body().getResponse().getBody().getMMSTORAGERACKLIST()){
                        storagePackList.add(data.getNAME()+"/"+data.getDESCRIPTION()+"/"+data.getCATEGORY());
                        storagePackRRN.add(data.getOBJECTRRN());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<WithdrawalEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"MLOTBACK");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERYSTORAGEPACK");
        map.addProperty("WAREHOUSERRN",rrn);
        NetUtils.requestPostNet("IF.MLOTBACK","Common", WithdrawalActivity.this,map,callBack);
    }


}
